﻿using AntDesign;
using AntDesign.Core.Extensions;
using AntDesign.JsInterop;
using EmployeeLeaveSystem.Client.Pages;
using EmployeeLeaveSystem.Shared.DAO;
using EmployeeLeaveSystem.Shared.Models;
using Microsoft.AspNetCore.Components;
using System.ComponentModel;
using System.Reflection;
using static EmployeeLeaveSystem.Client.GobalManager;

namespace EmployeeLeaveSystem.Client {
    public class GobalManager {
        public static GobalManager Instance { get; private set; }
        [Inject]
        public static NavigationManager? NavigationManager { get; set; }
        private readonly HttpClient _httpClient; // 依赖注入的http端
        private readonly NavigationManager _nv; // 依赖注入，跳转访问器
        //static StaffInfo? _staffInfo = new() { Id = 4, Name = "小熊" };
        static StaffInfo? _staffInfo;
        public static StaffInfo? myInfo {
            get { return _staffInfo; }
            set {
                _staffInfo = value;
                if (GobalManager.Instance?.MyInfoCed != null) GobalManager.Instance?.MyInfoCed();
            }
        }
        // ====================委托通知开始的地方=====================
        public delegate void MyInfoChanged();
        MyInfoChanged _MyInfoChanged;
        public MyInfoChanged MyInfoCed {
            get {
                return _MyInfoChanged;
            }
            set {
                if (_MyInfoChanged == null || !_MyInfoChanged.GetInvocationList().Contains(value)) {
                    _MyInfoChanged = value; // 务必写成等于号
                    Console.WriteLine("【GobalManager】接受到了委托注册请求：" + value.GetMethodInfo().Name);
                    Console.WriteLine("【GobalManager】现在的委托列表长度：" + _MyInfoChanged.GetInvocationList().Length);
                } else {
                    Console.WriteLine("【GobalManager】当前方法已经在委托行列中" + value.Method.Name + " 现在开始清除并且重新注册");
                    foreach (var item in _MyInfoChanged.GetInvocationList()) {
                        if (item.GetMethodInfo().Name == value.Method.Name) {
                            Console.WriteLine("找到了重复注册的委托：" + item.GetMethodInfo().Name);
                        }
                    }
                }
            }
        } // 当用户信息被刷新的时候，通知组件的行为
        public void DisposeMyInfoCed(MyInfoChanged value) {
            if (_MyInfoChanged != null && _MyInfoChanged.GetInvocationList().Contains(value)) {
                Console.WriteLine("【GobalManager】现在的注册函数数量：" + _MyInfoChanged.GetInvocationList().Length);

                _MyInfoChanged -= value;

                Console.WriteLine("【GobalManager】退出注册的函数：" + value.GetMethodInfo().Name);
                Console.WriteLine("【GobalManager】现在的注册函数数量：" + _MyInfoChanged?.GetInvocationList().Length);
            } else {
                Console.WriteLine("【GobalManager】退出注册失败了");
            }
        }

        static List<IPageFlush> pages = new(); // 一个全局列表，用于刷新
        public static List<StaffInfo> staffInfos = new();
        public GobalManager(HttpClient httpClient, NavigationManager nv) {
            this._httpClient = httpClient; _nv = nv;
            Instance = this;
        }
        public static void AddPage(IPageFlush page) {
            if (!pages.Contains(page)) pages.Add(page);
        }

        public static async void FlushPages() {
            foreach (var page in pages) {
                await Task.Run(() => { page.Flush(); });
            }
        }

    }
}
